package com.example.yunhailutong.zhongtan.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.yunhailutong.zhongtan.R;
import com.example.yunhailutong.zhongtan.activity.ActivityDingDanDesc;
import com.example.yunhailutong.zhongtan.activity.ActivityXuQiuDesc;
import com.example.yunhailutong.zhongtan.app.App;
import com.example.yunhailutong.zhongtan.app.OldsEvent;
import com.example.yunhailutong.zhongtan.entity.DingDanListEntity;
import com.example.yunhailutong.zhongtan.entity.XuQiuEntity;
import com.example.yunhailutong.zhongtan.net.PersionBiz;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.squareup.picasso.Picasso;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by yunhailutong on 2018/4/26.
 */

@SuppressLint("ValidFragment")
public class FragmentList extends BaseFragment {
    Unbinder unbinder;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private View view;
    private String ordertype;

    @SuppressLint("ValidFragment")
    public FragmentList(String ordertype) {
        this.ordertype = ordertype;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = inflater.inflate(R.layout.fragment_baselistview, container, false);
        // TODO: add setContentView(...) invocation
        unbinder=ButterKnife.bind(this, view);
        if(!EventBus.getDefault().isRegistered(this)){//加上判断
            EventBus.getDefault().register(this);
        }
        Map<String, String> params = new HashMap<>();
        params.put("pageIndx", "1");
        params.put("pageSize","100");
        params.put("phone",App.number);
        params.put("orderType", ordertype);
        params.put("token", App.token);
        new PersionBiz().GetDingDanList(params,ordertype);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                Map<String, String> params = new HashMap<>();
                params.put("pageIndx", "1");
                params.put("pageSize","100");
                params.put("phone",  App.number);
                params.put("orderType", ordertype);
                params.put("token", App.token);
                new PersionBiz().GetDingDanList(params,ordertype);
                //传入false表示刷新失败
                refreshlayout.finishRefresh();
            }
        });
        return view;
    }
    private void initListView(final DingDanListEntity entity) {
        listview.setAdapter(new CommonAdapter<DingDanListEntity.DataBean>(getActivity(), R.layout.main_item_dingdan, entity.getData()) {
            @Override
            protected void convert(ViewHolder viewHolder, DingDanListEntity.DataBean item, int position) {
                viewHolder.setText(R.id.bianhao,item.getOrderNo());
                viewHolder.setText(R.id.chepaihao,item.getCarNo());
                viewHolder.setText(R.id.gongsimingcheng,item.getFname());
                viewHolder.setText(R.id.time,item.getOrderDate());
                viewHolder.setText(R.id.text1,item.getPname());
                viewHolder.setText(R.id.text2,item.getNetWeight()+"吨");
                if(item.getLevel() != null){
                    viewHolder.setText(R.id.text3,item.getLevel()+"级");
                }
                switch (ordertype){
                    case "0":
                        viewHolder.setText(R.id.state,"待质检");
                        break;
                    case "1":
                        viewHolder.setText(R.id.state,"待确认");
                        break;
                    case "2":
                        viewHolder.setText(R.id.state,"待付款");
                        break;
                    case "3":
                        viewHolder.setText(R.id.state,"已付款");
                        break;
                    case "4":
                        viewHolder.setText(R.id.state,"异常");
                        break;

                }
            }
        });
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ActivityDingDanDesc.class);
                intent.putExtra("id",entity.getData().get(position).getOrderId());
                switch (ordertype){
                    case "0":
                        intent.putExtra("state","待质检");
                        break;
                    case "1":
                        intent.putExtra("state","待确认");
                        break;
                    case "2":
                        intent.putExtra("state","待付款");
                        break;
                    case "3":
                        intent.putExtra("state","已付款");
                        break;
                    case "4":
                        intent.putExtra("state","异常");
                        break;
                }
                startActivity(intent);
            }

        });
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
    @Subscribe
    public void log(OldsEvent.DingDanEvent msg) {
        if (msg.what == -1) {
            disPlay(msg.obj.toString());
        }
        if (msg.what == 1) {
            if(msg.arg1.equals(ordertype)){
                initListView((DingDanListEntity) msg.obj);
            }
        }
    }

    @Override
    public void onClick(View v) {

    }
}
